﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Transactions;

public partial class Main_Inventory_ServiceMaster : System.Web.UI.Page
{
    string sql = string.Empty;
    Datautility du = new Datautility();
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {

            if (Request.QueryString["action"] != null)
            {
                if (Request.QueryString["action"].ToString() == "addnew")
                {
                    PnlAddNew.Visible = true;
                    BindTaxablity();
                    BindBasisOfTaxablity();
                    BindCategory();
                    BindDdlSubclass();
                    BindDdlAdditionTax();
                    BindDdlHeadofAccount();

                }
                else if (Request.QueryString["action"].ToString() == "view")
                {

                    PnlView.Visible = true;
                    BindGridService();
                }
            }
        }
    }

    protected void BindTaxablity()
    {
        DdlTaxablity.Items.Insert(0, new ListItem("Taxable", "0"));
        DdlTaxablity.Items.Insert(1, new ListItem("Non-Taxable", "1"));
        DdlTaxablity.Items.Insert(2, new ListItem("Expense Reimbursement", "2"));
        DdlTaxablity.SelectedIndex = 0;
    }

    protected void BindDdlSubclass()
    {
        DdlSubclass.Items.Insert(0, new ListItem("Domestic", "1"));
        DdlSubclass.Items.Insert(1, new ListItem("Export", "2"));
        DdlSubclass.Items.Insert(2, new ListItem("Exempted", "3"));
        DdlSubclass.Items.Insert(3, new ListItem("Others", "4"));
    }

    protected void BindDdlAdditionTax()
    {
        DataTable dt = du.getdatausingprocedure("USP_IVC_GetAdditionalTax");
        if (dt.Rows.Count > 0)
        {
            DdlAdditionTax.DataSource = dt;
            DdlAdditionTax.DataTextField = "MAINCLASSNAME";
            DdlAdditionTax.DataValueField = "mainclassid";
            DdlAdditionTax.DataBind();
            DdlAdditionTax.Items.Insert(0, new ListItem("", "0"));
            DdlAdditionTax.SelectedIndex = 0;
        }
    }

    protected void bindtax()
    {
        SqlParameter[] parm = {
                                  new SqlParameter("@mainclassid",DdlAdditionTax.SelectedValue) 
                              };
        DataTable dt = du.getdatausingprocedure("usp_ivc_Bindtaxrate", parm);
        if (dt.Rows.Count > 0)
        {
            TxtTaxRate.Text = dt.Rows[0]["DEFTAXRATE"].ToString();
        }
        else
        {
            TxtTaxRate.Text = string.Empty;
        }
    }

    protected void BindBasisOfTaxablity()
    {
        if (DdlTaxablity.SelectedValue == "0")
        {
            PanelTaxablity.Visible = true;
            PanelExpense.Visible = false;
            //PanelNonTaxable.Visible = false;
            DdlAdditionTax.Enabled = true;
            TxtServicetax.Enabled = true;
            PanelTax.Enabled = true;
            GetCurrentServiceTaxRate(DateTime.Now.ToShortDateString());
        }
        if (DdlTaxablity.SelectedValue == "1")
        {
            PanelExpense.Visible = false;
            //PanelNonTaxable.Visible = true;
            PanelTaxablity.Visible = false;
            DdlAdditionTax.Enabled = false;
            TxtServicetax.Enabled = false;
            PanelTax.Enabled = false;
            TxtRate.Text = string.Empty;
            TxtPeCess.Text = string.Empty;
            TxtHecess.Text = string.Empty;
            TxtServicetax.Text = string.Empty;
            DdlAdditionTax.SelectedItem.Text = string.Empty;
            TxtTaxRate.Text = string.Empty;
            TxtExempted.Text = string.Empty;
            PanelExempted.Visible = false;
        }
        if (DdlTaxablity.SelectedValue == "2")
        {
            //PanelNonTaxable.Visible = false;
            PanelTaxablity.Visible = false;
            PanelExpense.Visible = true;
            DdlAdditionTax.Enabled = false;
            TxtServicetax.Enabled = false;
            PanelTax.Enabled = false;
        }
    }

    protected void DdlTaxablity_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindBasisOfTaxablity();
    }

    protected void BindCategory()
    {
        sql = "select SERVICECATEGORYID, SERVICECATEGORY from IVC_SERVICECATEGORY";
        DataTable dt = du.getdata(sql);
        if (dt.Rows.Count > 0)
        {
            DdlCategory.DataTextField = "SERVICECATEGORY";
            DdlCategory.DataValueField = "SERVICECATEGORYID";
            DdlCategory.DataSource = dt;
            DdlCategory.DataBind();
            DdlCategory.Items.Insert(0, new ListItem("", "0"));
        }
    }

    protected void BindDdlHeadofAccount()
    {
        DataTable dt = du.getdatausingprocedure("USP_IVC_GetHeadOfAccountServiceMaster");
        if (dt.Rows.Count > 0)
        {

            DdlHeadofAccount.DataSource = dt;
            DdlHeadofAccount.DataTextField = "LNAME";
            DdlHeadofAccount.DataValueField = "LID";
            DdlHeadofAccount.DataBind();
            DdlHeadofAccount.Items.Insert(0, new ListItem("", "0"));
            DdlHeadofAccount.SelectedIndex = 0;
        }
    }

    protected void DdlAdditionTax_SelectedIndexChanged(object sender, EventArgs e)
    {

        if (DdlAdditionTax.SelectedValue != "0")
        {
            bindtax();
        }
        if (DdlAdditionTax.SelectedIndex == 3 || DdlAdditionTax.SelectedIndex == 4 || DdlAdditionTax.SelectedIndex == 5)
        {
            PanelExempted.Visible = true;
        }
        else
        {
            PanelExempted.Visible = false;
        }
    }

    protected void Exemptedpanelvisible()
    {
        sql = "Select MAINCLASSNAME, Mainclassid from ACC_TAXMAINCLASS where TAXTYPEID = 11";
        DataTable dt = du.getdata(sql);
        if (dt.Rows.Count > 0)
        {
            PanelExempted.Visible = true;
        }
    }

    protected void InsertServiceMaster()
    {
        try
        {
            double exempted = 0;
            if (TxtExempted.Text != string.Empty)
            {
                exempted = Convert.ToDouble(TxtExempted.Text);
            }
            SqlParameter[] parm = {
                                  new SqlParameter("@SERVICENAME", TxtServiceName.Text.Trim()),
                                  new SqlParameter("@SERVICECATEGORYID", DdlCategory.SelectedValue),
                                  new SqlParameter("@ADDDESC",TxtAdditionDescription.Text.Trim()),
                                  new SqlParameter("@TAXABILITY",DdlTaxablity.SelectedValue),
                                  new SqlParameter("@LEDGERID",DdlHeadofAccount.SelectedValue),
                                  new SqlParameter ("@SERVICECLASSIFICATIONID",hidServiceId.Value),
                                  new SqlParameter("@EXPENSEREIMURSEMENT",Radioexpense.SelectedValue),
                                  new SqlParameter("@TAXTYPEID",DdlAdditionTax.SelectedValue),
                                  new SqlParameter("@TAXCALON",RadioApplyTax.SelectedValue),
                                  new SqlParameter("@SUBCLASS",DdlSubclass.SelectedValue),
                                  //new SqlParameter("@ABATEMENTYN",""),
                                  new SqlParameter("@VATEXMPRATE",exempted),
                                  new SqlParameter("@SERVICECHARGERATE",TxtService.Text.Trim()),
                                  new SqlParameter("@CNAME",Session["username"].ToString())

                              };

            du.executeprocedure("usp_IVC_InsertServicemaster", parm);
            lblMsg.Attributes.Remove("class");
            lblMsg.Attributes.Add("class", "true");
            lblMsg.Text = "Service Inserted";
        }
        catch (Exception ex)
        {
            throw ex;
        }


    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            using (TransactionScope scope = new TransactionScope())
            {
                InsertServiceMaster();
                scope.Complete();               
            }
            BindGridService();
        }
        catch (Exception ex)
        {
            lblMsg.Text = ex.Message;
        }
    }

    protected void BindGridService()
    {
        DataTable dt = du.getdatausingprocedure("USP_IVC_BindServicemaster");
        GrdService.DataSource = dt;
        GrdService.DataBind();
    }

    protected void DdlSubclass_SelectedIndexChanged(object sender, EventArgs e)
    {

        if (DdlSubclass.SelectedValue == "1")
        {
            GetCurrentServiceTaxRate(DateTime.Now.ToShortDateString());
            TxtTaxRate.Text = "0.00";
        }
        if (DdlSubclass.SelectedValue == "2")
        {
            TxtRate.Text = "0.00";
            TxtPeCess.Text = "0.00";
            TxtHecess.Text = "0.00";
            TxtTaxRate.Text = "0.00";
        }
        if (DdlSubclass.SelectedValue == "3")
        {
            TxtRate.Text = "0.00";
            TxtPeCess.Text = "0.00";
            TxtHecess.Text = "0.00";
            TxtTaxRate.Text = "0.00";
        }
        if (DdlSubclass.SelectedValue == "4")
        {
            TxtRate.Text = "0.00";
            TxtPeCess.Text = "0.00";
            TxtHecess.Text = "0.00";
            TxtTaxRate.Text = "0.00";
        }
    }

    private void GetCurrentServiceTaxRate(string strCurrentDate)
    {

        if (strCurrentDate != string.Empty)
        {
            string[] strTemp = strCurrentDate.ToString().Split('/');
            string strDate = strTemp[1] + "/" + strTemp[0] + "/" + strTemp[2];

            SqlParameter[] sparam ={
                                       new SqlParameter("@CURRENTDATE",strDate)
                                   };
            DataTable DTCurrentST = du.getdatausingprocedure("USP_IVC_GetCurrentServiceTaxRate", sparam);
            if (DTCurrentST.Rows.Count > 0)
            {
                TxtRate.Text = string.Format("{0:0.00}", Convert.ToDouble(DTCurrentST.Rows[0]["ServiceTax"].ToString())).ToString();
                TxtPeCess.Text = string.Format("{0:0.00}", Convert.ToDouble(DTCurrentST.Rows[0]["PCess"].ToString())).ToString();
                TxtHecess.Text = string.Format("{0:0.00}", Convert.ToDouble(DTCurrentST.Rows[0]["HCess"].ToString())).ToString();
            }
        }
    }

    protected void GrdService_SelectedIndexChanged(object sender, EventArgs e)
    {
        string service = GrdService.SelectedDataKey.Value.ToString();
        Response.Redirect("ServiceMasterEdit.aspx?Servicekey=" + service);
    }


    protected void GrdService_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GrdService.PageIndex = e.NewPageIndex;
        BindGridService();
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("ServiceMaster.aspx?action=view");
    }
}